package com.gljx.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.TurnexamCost;
import com.gljx.web.Entity.TurnexamCostEntry;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.TurnexamCostEntryMapper;
import com.gljx.web.dao.TurnexamCostMapper;
import com.gljx.web.service.TurnexamCostService;

@Service
@Transactional
public class TurnexamCostServiceImpl extends CommonDao implements TurnexamCostService{

	@Autowired
	private TurnexamCostMapper turnexamCostMapper;
	
	@Autowired
	private TurnexamCostEntryMapper turnexamCostEntryMapper;
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("Pindex", pindex);
		paramMap.put("Psql", psql);
		paramMap.put("Psize", psize);
		paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
		paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.TurnexamCostMapper.pagemap", paramMap, null);
		return  paramMap;
	}

	/**
	  取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @param Pcount 记录总数
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("Psql", psql);
		paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
		this.getSqlSession().select("com.gljx.web.dao.TurnexamCostMapper.pagecount", paramMap, null);
		return paramMap;
	}

	/**
	 * 根据父类地查询
	 */
	@Override
	public List<TurnexamCostEntry> queryTurnexamCostEntry(String parentId,String studentName,String cfidcardno,String cfmobile) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("v_fparentid", parentId);
		paramMap.put("v_studentName", studentName);
		paramMap.put("v_cfmobile", cfmobile);
		paramMap.put("v_cfidcardno", cfidcardno);
		paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.TurnexamCostEntryMapper.selectTurnexamCostEntry", paramMap, null);
		@SuppressWarnings("unchecked")
		List<TurnexamCostEntry> turnexamCostEntrys = (List<TurnexamCostEntry>) paramMap.get("v_cursor");
		return turnexamCostEntrys;
	}

	/**
	 * 实现添加
	 */
	@Override
	public void addTurnexamCost(TurnexamCost turnexamCost, String[] fseq, String[] cfstudentno, String[] cfstudentid,
			String[] cfidcardno, String[] cfsex, String[] cfsqcx, String[] cfmobile, String[] cfamount, String[] cffk,
			String[] cfguazhang, String[] cffpsj, String[] cffphm, String[] cfremark) {
		// TODO Auto-generated method stub
		//添加父类
		turnexamCostMapper.insertSelective(turnexamCost);
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				TurnexamCostEntry turnexamCostEntry = new TurnexamCostEntry();
				turnexamCostEntry.setCfstudentno(cfstudentno[i]);
				turnexamCostEntry.setCfstudentid(cfstudentid[i]);
				turnexamCostEntry.setCfidcardno(cfidcardno[i]);
				turnexamCostEntry.setCfsex(Long.parseLong(cfsex[i]));
				turnexamCostEntry.setCfsqcx(cfsqcx[i]);
				turnexamCostEntry.setCfmobile(cfmobile[i]);
				if("".equals(cfamount[i])){

				}else{
					turnexamCostEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
				}
				if("".equals(cffk[i])){
					
				}else{
					turnexamCostEntry.setCffk(BigDecimalFormat.formot(cffk[i]));
				}
				if("".equals(cfguazhang[i])){
					
				}else{
					turnexamCostEntry.setCfguazhang(BigDecimalFormat.formot(cfguazhang[i]));
				}
				turnexamCostEntry.setCffpsj(cffpsj[i]);
				turnexamCostEntry.setCffphm(cffphm[i]);
				turnexamCostEntry.setCfremark(cfremark[i]);
				turnexamCostEntry.setFid(GetFid.UUIDIntoFid());
				turnexamCostEntry.setFparentid(turnexamCost.getFid());
				turnexamCostEntry.setFseq((long) (i+1));
				//添加子类
				turnexamCostEntryMapper.insertSelective(turnexamCostEntry);
			}
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public TurnexamCost queryByFid(String fid) {
		// TODO Auto-generated method stub
		return turnexamCostMapper.selectByFid(fid);
	}

	/**
	 * 实现修改
	 */
	@Override
	public void mofifyTurnexamCost(TurnexamCost turnexamCost, String[] fseq, String[] cfstudentno, String[] cfstudentid,
			String[] cfidcardno, String[] cfsex, String[] cfsqcx, String[] cfmobile, String[] cfamount, String[] cffk,
			String[] cfguazhang, String[] cffpsj, String[] cffphm, String[] cfremark) {
		// TODO Auto-generated method stub
		//修改父类
		turnexamCostMapper.updateByPrimaryKeySelective(turnexamCost);
		//先删除子类信息
		turnexamCostEntryMapper.deleteByFparentId(turnexamCost.getFid());
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				TurnexamCostEntry turnexamCostEntry = new TurnexamCostEntry();
				turnexamCostEntry.setCfstudentno(cfstudentno[i]);
				turnexamCostEntry.setCfstudentid(cfstudentid[i]);
				turnexamCostEntry.setCfidcardno(cfidcardno[i]);
				turnexamCostEntry.setCfsex(Long.parseLong(cfsex[i]));
				turnexamCostEntry.setCfsqcx(cfsqcx[i]);
				turnexamCostEntry.setCfmobile(cfmobile[i]);
				if("".equals(cfamount[i])){

				}else{
					turnexamCostEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
				}
				if("".equals(cffk[i])){
					
				}else{
					turnexamCostEntry.setCffk(BigDecimalFormat.formot(cffk[i]));
				}
				if("".equals(cfguazhang[i])){
					
				}else{
					turnexamCostEntry.setCfguazhang(BigDecimalFormat.formot(cfguazhang[i]));
				}
				turnexamCostEntry.setCffpsj(cffpsj[i]);
				turnexamCostEntry.setCffphm(cffphm[i]);
				turnexamCostEntry.setCfremark(cfremark[i]);
				turnexamCostEntry.setFid(GetFid.UUIDIntoFid());
				turnexamCostEntry.setFparentid(turnexamCost.getFid());
				turnexamCostEntry.setFseq((long) (i+1));
				//添加子类
				turnexamCostEntryMapper.insertSelective(turnexamCostEntry);
			}
		}
	}

	/**
	 * 实现删除
	 */
	@Override
	public void deleteTurnexamByFid(String[] deleteId, String fid) {
		// TODO Auto-generated method stub
		if (fid == null || "".equals(fid)) {
			for (int i = 0; i < deleteId.length; i++) {
				//先删除子类
				turnexamCostEntryMapper.deleteByFparentId(deleteId[i]);
				//在删除父类
				turnexamCostMapper.deleteByPrimaryKey(deleteId[i]);
			}
		}else{
			//先删除子类
			turnexamCostEntryMapper.deleteByFparentId(fid);
			//在删除父类
			turnexamCostMapper.deleteByPrimaryKey(fid);
		}
	}

	/**
	 * 实现审核
	 */
	@Override
	public int auditTurnexamCostByFid(TurnexamCost turnexamCost) {
		// TODO Auto-generated method stub
		return turnexamCostMapper.updateByPrimaryKeySelective(turnexamCost);
	}

	@Override
	public List<TurnexamCostEntry> selectTurnexamCostByInfo(String fcompanyid, String studentName, String cfidcardno, String cfmobile, String beginTime, String endTime) {
		Map<String, String> map = new HashMap<>();
		map.put("studentName", studentName);
		map.put("cfidcardno", cfidcardno);
		map.put("cfmobile", cfmobile);
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("fcompanyid", fcompanyid);
		return turnexamCostEntryMapper.selectTurnexamCostByInfo(map);
	}
}
